home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / renaisnc / lib.lha / Lib / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-04  |  5.2 KB  |  232 lines

  1.  
  2. /*
  3. ** File:    tree.c
  4. ** Author:    Karen Foltz
  5. ** Last Modified:  10 Oct 1986
  6. ** Purpose:
  7. **    The module "tree" defines the tree data structure which describes
  8. **    objects in the Constructive Solid Geometry modelling system, and
  9. **     a set of operations on these data structures.  The set of operations
  10. **     defined are used to build and to print the contents of a CSG tree.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "4D.h"
  15. #include "tree.h"
  16.  
  17. /*Imports*/
  18. extern char *malloc();
  19.  
  20.  
  21. static char *PrimitiveObjNames[] = {
  22.   "sphere",
  23.   "cube",
  24.   "cone",
  25.   "cylinder",
  26.   "halfspace",
  27.   "torus",
  28.   "mesh",
  29. };
  30.  
  31. static char *OperatorNames[] = {
  32.   "Union",
  33.   "Intersection",
  34.   "Difference",
  35. };
  36.  
  37.  
  38. CSGtree *MakePrimitiveNode(shape, embed, invembed, surface)
  39.   PrimitiveShape shape;
  40.   TransformType4D embed, invembed;
  41.   MaterialDescr surface;
  42. {
  43.     CSGtree *newnode;
  44.     newnode = (CSGtree *) malloc(sizeof(CSGtree));
  45.     newnode->kind = Primitive;
  46.     newnode->embedding = embed;
  47.     newnode->invembedding = invembed;
  48.     newnode->var.p.shape = shape;
  49.     newnode->var.p.surface = surface;
  50.     return newnode;
  51. }
  52.  
  53. CSGtree *MakeCompositeNode(op, l_solid, r_solid, embed, invembed)
  54.   enum OpCodes op;
  55.   CSGtree *l_solid, *r_solid;
  56.   TransformType4D embed, invembed;
  57. {
  58.     CSGtree *newnode;
  59.     newnode = (CSGtree *) malloc(sizeof(CSGtree));
  60.     newnode->kind = Composite;
  61.     newnode->embedding = embed;
  62.     newnode->invembedding = invembed;
  63.     newnode->var.c.operation = op;
  64.     newnode->var.c.l_solid_ptr = l_solid;
  65.     newnode->var.c.r_solid_ptr = r_solid;
  66.     return newnode;
  67. }
  68.  
  69. CSGtree *EmbedNode(node, embed, invembed)
  70.   CSGtree *node;
  71.   TransformType4D embed,invembed;
  72. {
  73.     node->embedding = TxT4D(embed,node->embedding);
  74.     node->invembedding = TxT4D(node->invembedding,invembed);
  75.     return node;
  76. }
  77.  
  78. CSGtree *DuplicateTree(tree)
  79.   CSGtree *tree;
  80. {
  81.     CSGtree *newtree, oldtree;
  82.     oldtree = *tree;
  83.     newtree = (CSGtree *) malloc(sizeof(CSGtree));
  84.     newtree->kind = oldtree.kind;
  85.     newtree->embedding = oldtree.embedding;
  86.     newtree->invembedding = oldtree.invembedding;
  87.     if (oldtree.kind == Primitive) {
  88.         newtree->var.p.shape = oldtree.var.p.shape;
  89.         newtree->var.p.surface = oldtree.var.p.surface;
  90.     } else {
  91.         newtree->var.c.operation = oldtree.var.c.operation;
  92.         newtree->var.c.l_solid_ptr = 
  93.             DuplicateTree(oldtree.var.c.l_solid_ptr);
  94.         newtree->var.c.r_solid_ptr = 
  95.             DuplicateTree(oldtree.var.c.r_solid_ptr);
  96.     }
  97.     return newtree;
  98. }
  99.  
  100.  
  101. CSGtree *SetSurface(node,surface)
  102.   CSGtree *node;
  103.   MaterialDescr surface;
  104. {
  105.     if (node->kind == Composite) {
  106.         fprintf(stderr,"ERROR in SetSurface: node is not Primitive.\n");
  107.         exit(-1);
  108.     }
  109.     node->var.p.surface = surface;
  110.     return node;
  111. }
  112.  
  113. void PrintOpcode(f,op)
  114.   enum OpCodes op;
  115.   FILE *f;
  116. {
  117.   fprintf(f, "%s\n", OperatorNames[(int) op]);
  118. }
  119.  
  120. void PrintPrimitiveObject(f,object)
  121.   FILE *f;
  122.   enum PrimitiveObjects object;
  123. {
  124.   fprintf(f, "%s\n", PrimitiveObjNames[(int) object]);
  125. }
  126.  
  127. void PrintNodeType(f,type)
  128.   FILE *f;
  129.   enum NodeTypes type;
  130. {
  131.     if (type == Primitive)
  132.         fprintf(f,"Primitive\n");
  133.     else
  134.         fprintf(f,"Composite\n");
  135. }
  136.  
  137. /*
  138.  * PrintMaterial:
  139.  *   Print out the record describing a material.  Prints the texture
  140.  * function name and what its parameters are, and the pair of embedding
  141.  * matrices that do the transformations from texture to object space and back.
  142.  */
  143. void
  144. PrintMaterial(f, material)
  145. FILE *f;
  146. MaterialDescr material;
  147. {
  148.   fprintf(f,"Material Description:\n");
  149.   fprintf(f, "Material is%s an emitter.\n", material.emitter ? "" : " not");
  150.   fprintf(f, "embedding matrix into texture space:\n");
  151.   PrintTransform4D(f, material.embedding);
  152.   fprintf(f, "inverse embedding matrix:\n");
  153.   PrintTransform4D(f, material.inv_embedding);
  154.   fprintf(f, "function: \n");
  155.   if (material.texture) {
  156.     fPrintToken(f, material.texture);
  157.     fprintf(f, "\n");
  158.   } else 
  159.     fprintf(f, "NONE\n");
  160. }
  161.  
  162.  
  163.  
  164. void PrintNodeSurface(f,node)
  165.   FILE *f;
  166.   CSGtree *node;
  167. {
  168.   if (node->kind == Composite) {
  169.     fprintf(stderr,"ERROR in PrintNodeSurface: node is not Primitive.\n");
  170.     exit(-1);
  171.   }
  172.   
  173.   PrintMaterial(f,node->var.p.surface);
  174. }
  175.  
  176.  
  177. void PrintBoundVolume(f,bound_volume)
  178.   FILE *f;
  179.   BoundingVolume bound_volume;
  180. {
  181.   int i;
  182.  
  183. #ifdef BOUNDING_SPHERES
  184.     fprintf(f,"Bounding Volume: radius = %7.2f  center = ",
  185.             bound_volume.radius);
  186.     PrintPoint4D(f, bound_volume.center);
  187. #else
  188.     fprintf( f, "\
  189. Bounding Volume: (xmin,xmax) (ymin,ymax) (zmin,zmax)\n\
  190.                  \n");
  191.     for(i=0; i<3; i++)
  192.       fprintf( f,"(%f,%f) ", bound_volume.I[i].min, bound_volume.I[i].max );
  193. #endif
  194. }
  195.  
  196. unsigned int PrintNode(f,count,ptr)
  197.   FILE *f;
  198.   unsigned int count;
  199.   CSGtree *ptr;
  200. {
  201.     fprintf(f,"\nNode: %u \t",count);
  202.     if (ptr->kind == Composite) {
  203.         fprintf(f,"Composite Node, operator = ");
  204.         PrintOpcode(f, ptr->var.c.operation );
  205.     } else {
  206.         fprintf(f,"Primitive Node, ");
  207.         PrintPrimitiveObject(f, ptr->var.p.shape.shape );
  208.         PrintNodeSurface(f,ptr);
  209.     }
  210.     fprintf(f,"embedding matrix:\n");
  211.     PrintTransform4D(f, ptr->embedding);
  212.     fprintf(f,"inverse embedding matrix:\n");
  213.     PrintTransform4D(f, ptr->invembedding);
  214.     PrintBoundVolume(f, ptr->bound_volume);
  215.     fprintf(f,"\n");
  216.     if (ptr->kind == Composite) {
  217.         count = PrintNode(f,count+1,ptr->var.c.l_solid_ptr);
  218.         count = PrintNode(f,count+1,ptr->var.c.r_solid_ptr);
  219.     }
  220.     return count;
  221. }
  222.  
  223. void PrintCSGtree(f,ptr)
  224.   CSGtree *ptr;
  225.   FILE *f;
  226. {
  227.     fprintf(f,"\nPre-order traversal of CSG tree:\n");
  228.     PrintNode(f,0,ptr);
  229.     fprintf(f,"End of Tree.\n");
  230. }
  231.  
  232.